package com.joke.mydiary;


import android.app.Activity;
import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.DialogFragment;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

import com.joke.mydiary.pickers.DatePickerFragment;
import com.joke.mydiary.pickers.TimePickerFragment;

import java.io.Serializable;
import java.util.Calendar;

public class RemindManagerActivity extends Activity implements Serializable, TimePickerDialog.OnTimeSetListener, DatePickerDialog.OnDateSetListener {

    private Calendar calendar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_remind);
        calendar = Calendar.getInstance();
        DialogFragment timeFragment = TimePickerFragment.getTimePickerFragmet(this);
        timeFragment.show(getFragmentManager(), "timePick");
        DialogFragment dateFragment = DatePickerFragment.getDatePickerFragment(this);
        dateFragment.show(getFragmentManager(), "datePicker");

    }

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        calendar.set(year, monthOfYear, dayOfMonth);
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
        calendar.set(Calendar.MINUTE, minute);
        long triggerTime= calendar.getTime().getTime();
        if(triggerTime<=System.currentTimeMillis())
        {
            Toast.makeText(this, "时间设置不合法！", Toast.LENGTH_SHORT).show();
             finish();
            return;
        }
        //发送闹钟服务
        AlarmManager alarmManager= (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent=getIntent();
        intent.setClass(this,AlarmShowActivity.class);
        PendingIntent pi=PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager.set(AlarmManager.RTC_WAKEUP,triggerTime,pi);
        Toast.makeText(this, "提醒设置成功！", Toast.LENGTH_SHORT).show();
        finish();
    }

    public void cancelMessage() {
        Toast.makeText(this, "您取消了设置提醒！", Toast.LENGTH_SHORT).show();
        finish();
    }


}
